package question_answer

import (
	"fmt"
	"news_for_deepseek_interface/config"
	questionAnswerImportDto "news_for_deepseek_interface/dto/admin/question_answer_import"
	"news_for_deepseek_interface/model"
)

func List(params questionAnswerImportDto.ListRequest) (response questionAnswerImportDto.ListResponse, err error) {
	db := model.GetInstance().GetDB()

	// 获取总数
	var total int64
	if err = db.Model(&model.FilesModel{}).Count(&total).Error; err != nil {
		err = fmt.Errorf("数量查询失败%s", err.Error())
		return
	}

	// 分页查询
	offset := (params.Page - 1) * params.Limit
	var data []model.FilesModel
	if err = db.Model(&model.FilesModel{}).
		Offset(offset).
		Limit(params.Limit).
		Order("created_at DESC").
		Find(&data).Error; err != nil {
		err = fmt.Errorf("数据查询失败%s", err.Error())
		return
	}

	// 构建响应
	response = questionAnswerImportDto.ListResponse{
		List:  make([]questionAnswerImportDto.QuestionAnswerImportRecord, 0),
		Total: total,
		Page:  params.Page,
		Limit: params.Limit,
	}

	for _, v := range data {
		errFile := v.ErrFile
		if errFile != "" {
			errFile = config.DefaultConfig.Server.ImageUrl + errFile
		}
		response.List = append(response.List, questionAnswerImportDto.QuestionAnswerImportRecord{
			Id:        v.Id,
			FilePath:  v.FilePath,
			FileName:  v.FileName,
			ErrFile:   errFile,
			Status:    v.Status,
			CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}

	return
}
